package com.wpzf.zxhc.controller;

import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.log.annotation.Log;
import com.wpzf.common.log.enums.BusinessType;
import com.wpzf.zxhc.utils.OBSUploadUnits;
import com.wpzf.zxhc.service.WpzfTbShengTJService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


//广西 中转层 省级上传附件 OBS
@RestController
@RequestMapping("/wpzffile")
public class WpzfTbShengTJController {

    @Autowired
    WpzfTbShengTJService wpzfTbShengTJService;
    /**
     * 文件上传
     *
     * @param file
     * @param dkbh
     * @param xzqdm
     * @return
     */
    @Log(title = "省份文件上传", businessType = BusinessType.OTHER)
    @Transactional
    @RequestMapping(value = "/uploadfjOBSFile", method = RequestMethod.POST)
    public AjaxResult uploadfjOBSFile(@RequestParam(value = "file") MultipartFile file,
                                    @RequestParam(value = "dkbh") String dkbh,
                                    @RequestParam(value = "xzqdm") String xzqdm,
                                    @RequestParam(value = "pch") String pch,
                                    @RequestParam(value = "year") String year,
                                    @RequestParam(value = "fjlx") String fjlx,
                                    @RequestParam(value = "username") String username) {
        try {
            if (file == null) {
                return AjaxResult.error("请选择需要上传的文件");
            } else {
                if (!file.isEmpty()) {
                    String originName = file.getOriginalFilename();
                    // 创建保存路径
                    String savePath = System.getProperty("user.dir") + "\\" + "shjtbresult" + "\\" + pch + "\\" + xzqdm + "\\" + dkbh;
                    // 保存文件的文件夹
                    File folder = new File(savePath);
                    // 判断路径是否存在,不存在则自动创建
                    if (!folder.exists()) {
                        folder.mkdirs();
                    }
                    String saveName = originName;

                    //文件存到本地
                    file.transferTo(new File(folder, saveName));
                    //文件地址
                    String filePath = savePath + "\\" + saveName;
                    String OBSAddress = "S/wpzf" + year + "/FJData/" + pch + "/" + xzqdm + "/" + dkbh + "/" + saveName;
                    String log = OBSUploadUnits.OBSUpload(OBSAddress, filePath);
                    deleteFolder(folder);
                    Map<String, Object> rest = new HashMap<>();
                    if ("success".equals(log)) {
                        String fj = "https://obs-top.obs.cn-north-1.myhuaweicloud.com/" + OBSAddress;
                        rest.put("fj", fj);
                        //提交成功 插入数据库表
                        Map<String, Object> map=new HashMap<>();
                        map.put("dkbh",dkbh);
                        map.put("pch",pch);
                        map.put("xzqdm",xzqdm);
                        map.put("fjmc",saveName);
                        map.put("fjurl",fj);
                        map.put("fjlx",fjlx);
                        map.put("uploadusername",username);
                        map.put("uploadtime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));


                        int k=wpzfTbShengTJService.insert(map);
                        return AjaxResult.success("附件文件上传成功", rest,k);
                    } else {
                        return AjaxResult.error("附件文件上传失败", rest);
                    }
                }
                return AjaxResult.error("文件上传失败");
            }
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("上传文件异常，请联系管理员" + ex);
        }
    }
    public static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteFolder(file);
                }
            }
        }
        folder.delete();
    }
    @Log(title = "文件上传", businessType = BusinessType.OTHER)
    @Transactional
    @RequestMapping(value = "/uploadzpOBSFile", method = RequestMethod.POST)
    public AjaxResult uploadzpOBSFile(@RequestParam(value = "file") MultipartFile file,
                                    @RequestParam(value = "dkbh") String dkbh,
                                    @RequestParam(value = "xzqdm") String xzqdm,
                                    @RequestParam(value = "pch") String pch,
                                    @RequestParam(value = "year") String year,
                                    @RequestParam(value = "username") String username) {
        try {
            if (file == null) {
                return AjaxResult.error("请选择需要上传的文件");
            } else {
                if (!file.isEmpty()) {
                    String originName = file.getOriginalFilename();
                    // 创建保存路径
                    String savePath = System.getProperty("user.dir") + "\\" + "shjtbresult" + "\\" + pch + "\\" + xzqdm + "\\" + dkbh;
                    // 保存文件的文件夹
                    File folder = new File(savePath);
                    // 判断路径是否存在,不存在则自动创建
                    if (!folder.exists()) {
                        folder.mkdirs();
                    }
                    String saveName = originName;

                    //文件存到本地
                    file.transferTo(new File(folder, saveName));
                    //文件地址
                    String filePath = savePath + "\\" + saveName;
                    String OBSAddress = "S/wpzf" + year + "/ZPData/" + pch + "/" + xzqdm + "/" + dkbh + "/" + saveName;
                    String log = OBSUploadUnits.OBSUpload(OBSAddress, filePath);
                    deleteFolder(folder);
                    Map<String, Object> rest = new HashMap<>();
                    if ("success".equals(log)) {
                        String fj = "https://obs-top.obs.cn-north-1.myhuaweicloud.com/" + OBSAddress;
                        rest.put("fj", fj);

                        //提交成功 插入数据库表
                        Map<String, Object> map=new HashMap<>();
                        map.put("dkbh",dkbh);
                        map.put("pch",pch);
                        map.put("xzqdm",xzqdm);
                        map.put("zpmc",saveName);
                        map.put("zpurl",fj);
                        map.put("uploadusername",username);
                        map.put("uploadtime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
                        int k=wpzfTbShengTJService.insertzp(map);

                        return AjaxResult.success("照片上传成功", rest,k);
                    } else {
                        return AjaxResult.error("照片文件上传失败", rest);
                    }
                }
                return AjaxResult.error("文件上传失败");
            }
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("上传文件异常，请联系管理员" + ex);
        }
    }

}
